﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BlackBird.Models;
using BlackBird.ControllerHelpers;

namespace BlackBird.DataAccess
{
    public static class ProjectDataAccess
    {
		public static ProjectDM NewProject(NewProjectDM newprj)
        {
            var projectDm = BuildProject(newprj);

            using (blackbirdEntities dbContext = new blackbirdEntities())
            {
                //build contractor
                var contractorDb = contactor.Createcontactor(0);
                contractorDb.RealName = newprj.ContactorInfo.RealName;
                contractorDb.Address = newprj.ContactorInfo.Address;
                contractorDb.Tel = newprj.ContactorInfo.Tel;
                dbContext.contactor.AddObject(contractorDb);

                //project id 不需要设置，设置也不起作用，mysql会自动分配
                var guidangStatus = SupportTypeStatus.GuiDang.ToString();
                var user = dbContext.user.First(p => p.Name == projectDm.BasicInfo.CreateUserName);
                var status = dbContext.status.First(p => p.StatusType == guidangStatus);
                var projectDb = project.Createproject(0,
                    user.idUser,
                    status.idStatus,
                    contractorDb.idContactor);
                projectDb.Name = projectDm.BasicInfo.Name;
				projectDb.SuoXie = projectDm.BasicInfo.SuoXie;
                projectDb.Date =  Convert.ToDateTime(projectDm.BasicInfo.CreateDate);
                projectDb.Description = newprj.Description;

                //add to db
                dbContext.project.AddObject(projectDb);
                dbContext.SaveChanges();

                projectDm.BasicInfo.Status = projectDb.status.DisplayName;
                projectDm.Id = projectDb.idProject.ToString();
            }

			GlobalObjectBuffer.AllProjects.AllProjects.Add(projectDm);
            return projectDm;
        }



        public static ProjectDM BuildProject(NewProjectDM newprj)
        {
            return new ProjectDM()
            {
                //TODO: use the right image accroindg to Project type
                thumbnialurl = Links.Content.Images.project.example_PNG,
                ContactorInfo = newprj.ContactorInfo,
                BasicInfo = new ProjectBasicDM()
                {
                    Name = newprj.Name,
					SuoXie = newprj.SuoXie,
                    CreateDate = DateTime.Now.ToString(),
                    CreateUserName = newprj.UserName,
                }
            };
        }

        public static ProjectDM GetProject(string id)
        {
            //using (blackbirdEntities dbContext = new blackbirdEntities())
            //{
            //    var projects = dbContext.project;
            //    var projectDb = projects.ToList().First(p => p.idProject.ToString() == id);
            //    return BuildProject(projectDb);
            //}

            return GlobalObjectBuffer.AllProjects.AllProjects.First(p => p.Id == id);
        }

        public static void AddBijiaDanToProject(string projectId, string bijiadanId)
        {
            ProjectDM parent = GetProject(projectId);
            parent.BasicInfo.BiJiaDanCount++;

            //TODO， need to hookup the bijiadan and project
            //Project needs has a member to keep bijiadan id
        }

        public static void AddFuKuanDanToProject(string projectId)
        {
            ProjectDM parent = GetProject(projectId);
            parent.BasicInfo.FuKuanDanCount++;
        }
    }
}